<?php

class TournamentsController extends AppController {

	public $name = 'Tournaments';

	public $helpers = array();

	public $uses = array('Tournament', 'Court', 'Reservation');
	
	public function beforeFilter() {
		parent::beforeFilter();
		$this->Auth->allow('index', 'goToView', 'view');
	}
	
	
	public function isAuthorized($user) {
		if (in_array($user['role'], array('admin', 'superAdmin'))) {
			return true;
		}
		if(in_array($this->action, array('index', 'goToView', 'view'))){
			return true;
		}
		return false;
	}

	public $components = array('RequestHandler');
	
	public function index() {
		$this->set('tournaments', $this->Tournament->find('list', array(
			'fields'		=> array('Tournament.id', 'Tournament.name'),
		)));
	}
	
	public function goToView() {
		$tournament = $this->Tournament->find('first', array('conditions'=>array('id'=>$this->request->data['Tournament']['tournament_id'])));
		$this->redirect(array('controller'=>'Tournaments', 'action'=>'view', $tournament['Tournament']['name']));
	}
	
	public function view($name = null) {
		$data = $this->paginate('Reservation', array('Tournament.name'=>$name));
		$this->set('reservations', $data);
		$this->set('tournamentName', $name);		
	}
	
	public function addName() {
		if($this->request->is('post')) {
			$data = $this->request->data;
			if ($this->Tournament->save($data)) {
				$this->redirect(array('controller'=>'Tournaments', 'action'=>'add', $data['Tournament']['name']));	
			} else {
				$this->Session->setFlash('Nie udało się dodać turnieju', 'flash_failure');
				$this->redirect(array('controller'=>'Tournaments', 'action'=>'index'));
			}
			
		}
	}

	public function add($name = null) {
		$this->set('courts', $this->Court->find('list', array(
			'fields'		=> array('Court.id', 'Court.name'),
			'conditions'	=> array('Court.out_of_order' => '0')
		)));
		$data = $this->paginate('Reservation', array('Tournament.name'=>$name));
		$this->set('reservations', $data);		
	}
		
	public function save() {
		$data = $this->request->data;
		$end_time = $data['Tournament']['end_time'];
		unset($data['Tournament']['end_time']);
		
		$name = $data['Tournament']['tournament_name'];
		unset($data['Tournament']['tournament_name']);
		$tournament = $this->Tournament->find('first', array('conditions'=>array('Tournament.name' => $name)));
		$data['Tournament']['tournament_id'] = $tournament['Tournament']['id'];
		
		$reservations = array();

		while($data['Tournament']['start_time'] < $end_time) {
			$conflict = $this->Reservation->find('first', array('conditions'=>array(
																			'Reservation.court_id'=>$data['Tournament']['court_id'], 
																			'Reservation.start_time'=>$data['Tournament']['start_time'],
																			'Reservation.tournament_id'=>'')));
			if($conflict){
				$this->Reservation->id = $conflict['Reservation']['id'];
				$this->Reservation->delete();
																			
			}
			
			$conflict = $this->Reservation->find('first', array('conditions'=>array(
																			'Reservation.court_id'=>$data['Tournament']['court_id'], 
																			'Reservation.start_time'=>$data['Tournament']['start_time'])));
			if(!$conflict){
				array_push($reservations, $data['Tournament']);
			}
			$data['Tournament']['start_time'] = date("Y-m-d H:i:s", strtotime($data['Tournament']['start_time']. " +1 Hour"));
		}	
		if($this->Reservation->saveAll($reservations)) {
			$this->Session->setFlash('Rezerwacja dodana', 'flash_success');
		} else {
			$this->Session->setFlash('Nie udało się dodać rezerwacji', 'flash_failure');
		}
		$this->redirect(array('controller'=>'Tournaments', 'action'=>'add', $name));
	}
	
	public function delete($name = null) {
		$tournament = $this->Tournament->find('first', array('conditions'=>array('name'=>$name)));

		if($this->Tournament->delete($tournament['Tournament']['id'], true)) {
			$this->Session->setFlash('Turniej usunięty.', 'flash_success');
		} else {
			$this->Session->setFlash('Nie udało się usunąć turnieju.', 'flash_failure');
		}
		$this->redirect(array('controller'=>'Tournaments', 'action'=>'index'));  
	}

}
